package com.dyz.swordFingerOffer;

import java.util.*;

/**
 * @author: daiyizheng
 * @date: 2021/6/11 20:27
 * @description:
 */
public class Print32_2 {


    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(3);
        TreeNode node3 = new TreeNode(4);
        TreeNode node4 = new TreeNode(5);
        TreeNode node5 = new TreeNode(6);
        TreeNode node6 = new TreeNode(7);
        node2.left = node5;
        node2.right = node6;
        node1.left = node3;
        node1.right = node4;
        root.left = node1;
        root.right = node2;
        ArrayList<ArrayList<Integer>> print = new Print32_2().Print(root);
        System.out.println(print);

    }
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        if(pRoot==null){
            return ans;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(pRoot);
        int sum =1;

        while(!queue.isEmpty()){
            ArrayList<Integer> arr = new ArrayList<>();
            int pre = 0;
            while(sum>0){
                TreeNode node = queue.poll();
                arr.add(node.val);
                if(node.left!=null){
                    queue.add(node.left);
                    pre++;
                }
                if(node.right!=null){
                    queue.add(node.right);
                    pre++;
                }
                sum--;
            }
            ans.add(arr);
            sum = pre;
        }
        return ans;
    }



}
